/////////////////////////////////////////////////////////////////////////////
/*/	

/*/
/////////////////////////////////////////////////////////////////////////////
#include "Shooter.h"
#include "DefenseStruct.h"
#include "LevelManager.h"
#include "Base.h"
#include "CSGD_TextureManager.h"
#include "AnimInfo.h"
/////////////////////////////////////////////////////////////////////////////
// Function : Default Constructor
// Notes	: Gives the unit base traits
/////////////////////////////////////////////////////////////////////////////
CShooter::CShooter(){
	m_eType = SHOOT;
	SetHealth(100);
	SetMaxHealth(100);
	SetAttackCool(1);
	SetSpecAttCool(20);
	SetBaseDmg(10);
	SetSpecialDmg(20);
	SetAttackWait(1);
	SetSAttackWait(20);
	SetRange(100);
	SetImage( CSGD_TextureManager::GetInstance()->LoadTexture( _T("resource/Images/SingleSoldierTemp.png"), D3DCOLOR_ARGB(255,255,255,255) ) );
	SetWidth(64);
	SetHeight(64);

	/*AnimSystem = new CAnimationSystem();
	AnimSystem->LoadAnimation("NewAnimTest.xml");
	AnimationInfo = new CAnimInfo();
	if(AnimSystem->FindAnimation("Gundam_Walk") != -1)
	AnimationInfo->SetCurrAnim(AnimSystem->FindAnimation("Gundam_Walk"));
	else
	AnimationInfo->SetCurrAnim(0);
	AnimationInfo->SetOwner(this);

	CurrAnim = AnimSystem->GetAnimations()[AnimationInfo->GetCurrAnim()];
	CurrAnim->SetImageID(AnimSystem->GetAnimations()[AnimationInfo->GetCurrAnim()]->GetImageID());
	CurrAnim->Play(false,CurrAnim->GetIsLoop(),*AnimationInfo);*/
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Destructor
// Notes		: Cleans up any dynamically allocated memory
/////////////////////////////////////////////////////////////////////////////
CShooter::~CShooter()
{
	delete AnimationInfo;
	delete AnimSystem;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
// Parameters	: dT - Time elapsed since last update
// Notes		: Handles movement and behavior of the unit
/////////////////////////////////////////////////////////////////////////////
void CShooter::Update(float dT)
{
	//Use this for enemy queue setup

	priority_queue<CBaseUnit*> enemyQ;
	if(GetTarget() == nullptr)
	{
		std::vector<CBaseUnit*> swapVec = CBaseUnit::GetEnemiesInRange();
		if(swapVec.size() > 0)
		{
			for(unsigned int i = 0; i < swapVec.size();i++)
				enemyQ.push(swapVec[i]);


			//If so make sure they're in the same path (as clarity)
			//Add all in range enemies to queue
			//Access first target in queue
			//LOOP
			for(unsigned int qIter = 0; qIter < enemyQ.size(); )
			{
				CBaseUnit *_enemy = enemyQ.top();
				//Is It a Shielder
				//if(_enemy->GetType() == SHIELD)
				//{
				//	//YES
				//	//Break loop
				//	//Attack it
				//	SetTarget(_enemy);
				//	break;
				//}
				//Target's Health < 10%?
				if(_enemy->GetHealth() <= _enemy->GetMaxHealth() * 0.1f)
				{
					//YES
					//Break loop
					//Attack it
					SetTarget(_enemy);
					break;
				}
				else
				{
					//NO
					//is it a type you are strong against?
					if(_enemy->GetType() == MECH)
					{
						//YES
						//Break loop
						//Attack it
						SetTarget(_enemy);
						break;
					}
					else{
						//NO
						//Is it the last unit in queue?
						if(enemyQ.size() == 1)
						{
							//YES
							//Break loop
							//Attack it
							SetTarget(_enemy);
							break;
						}
						else
						{
							//NO
							//Access next target in queue
							enemyQ.pop();
							//Restart loop
						}
					}
				}
			}
		}
		else
		{
			//Check to see is shielder is nearby to attack first
			/*std::vector<CBaseUnit*> swapVec = GetEnemiesInRange();

			for (unsigned int i = 0; i < swapVec.size(); i++)
			{
			if(swapVec[i]->GetType() == SHIELD)\
			{
			SetTarget(swapVec[i]);
			break;
			}
			}*/

			//NO
			CDefenseStruct *_enemy = CBaseUnit::GetClosestGate();
			//Any structures in range (And paths)?
			//Check the waypoint for barricade/turret that's in range
			//_enemy = [what's at the spot]
			if(_enemy != NULL)
			{
				//YES
				//Attack it
				SPosition onePos;
				onePos.m_nPosX = (_enemy->GetPosX() + _enemy->GetWidth()/2);
				onePos.m_nPosY = (_enemy->GetPosY() + _enemy->GetHeight()/2);
				SPosition twoPos;
				twoPos.m_nPosX = (this->GetPosX() + GetWidth()/2);
				twoPos.m_nPosY = (this->GetPosY() + GetHeight()/2);

				bool bInFront = false;
				if( GetTeam() == 1 ) // it's to the right
				{
					if(_enemy->GetPosX() > GetPosX()) bInFront = true;
				}
				else
				{
					if(_enemy->GetPosX()+_enemy->GetWidth() < GetPosX()+GetWidth()) bInFront = true;
				}

				if(DistanceBetween(onePos,twoPos) < GetRange() && bInFront)
					SetTarget(_enemy);
			}
		}
	}
	else
	{
		//CBaseClass* TMP = GetTarget();
		//if(dynamic_cast<CBaseUnit*>(TMP)->GetHealth() < 0)
		//	SetTarget(nullptr);
	}
	CBaseUnit::Update(dT);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: Displays the unit to the screen
/////////////////////////////////////////////////////////////////////////////
void CShooter::Render()
{
	CBaseUnit::Render();
}